//
//  DAChangePassWordViewController.m
//  DAKnowledge
//
//  Created by DreamArts on 16/6/13.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAChangePassWordViewController.h"
#import "DAHeader.h"
#import "Utility.h"
#import "DADataHandel.h"
#import "DAAlertView.h"
@interface DAChangePassWordViewController ()<UITextFieldDelegate>{
    UIView *backView;
    UIView *accountView;
    UIView *confirmPassWordView;
    UIView *oldPassWordView;
    UIView *newPassWordView;
    UITextField *oldPassWordTextField;
    UITextField *newPassWordTextField;
    UITextField *confirmPassWordTextField;
}
@end

@implementation DAChangePassWordViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [oldPassWordTextField becomeFirstResponder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建输入框
    [self creatInputTextField];
}
#pragma mark - 创建输入框
- (void)creatInputTextField{
    backView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREENWIDTH, 44 * 3 + 1)];
    backView.backgroundColor = [UIColor whiteColor];
    backView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    backView.layer.borderWidth = 0.5;
    [self.view addSubview:backView];
    
    // 国际化字符串
    NSString *oldPassWord = NSLocalizedString(@"oldPassWorld", nil);
    NSString *newPassWord = NSLocalizedString(@"newPassWorld", nil);
    NSString *confirmPassWord = NSLocalizedString(@"confirmPassWorld", nil);
    NSString *inputOldPassWord = NSLocalizedString(@"inputOldPassWord", nil);
    NSString *inputNewPassWord = NSLocalizedString(@"inputNewPassWord", nil);
    NSString *inputConfirmPassWord = NSLocalizedString(@"inputConfirmPassWord", nil);
    
    
    // 原密码
    oldPassWordView  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 44)];
    oldPassWordView.backgroundColor = [UIColor whiteColor];
    [backView addSubview:oldPassWordView];
    
    UILabel *oldPassWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 140, 44)];
    oldPassWordLabel.font = [UIFont systemFontOfSize:14];
    oldPassWordLabel.text = oldPassWord;
    [oldPassWordView addSubview:oldPassWordLabel];
    
    oldPassWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(oldPassWordLabel.frame.origin.x + oldPassWordLabel.frame.size.width, 0, oldPassWordView.frame.size.width - (oldPassWordLabel.frame.origin.x + oldPassWordLabel.frame.size.width ) - 10, 44)];
    oldPassWordTextField.secureTextEntry = YES;
    oldPassWordTextField.placeholder = inputOldPassWord;
    oldPassWordTextField.font = [UIFont systemFontOfSize:14];
    oldPassWordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [oldPassWordTextField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [oldPassWordView addSubview:oldPassWordTextField];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, oldPassWordView.frame.size.height - 0.5, SCREENWIDTH, 0.5)];
    line1.backgroundColor = [UIColor lightGrayColor];
    [oldPassWordView addSubview:line1];
    
    // 新密码
    newPassWordView = [[UIView alloc]initWithFrame:CGRectMake(0, oldPassWordView.frame.origin.y + oldPassWordView.frame.size.height, SCREENWIDTH, 44)];
    newPassWordView.backgroundColor = [UIColor whiteColor];
    [backView addSubview:newPassWordView];
    
    UILabel *newPassWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 140, 44)];
    newPassWordLabel.font = [UIFont systemFontOfSize:14];
    newPassWordLabel.text = newPassWord;
    [newPassWordView addSubview:newPassWordLabel];
    
    newPassWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(newPassWordLabel.frame.origin.x + newPassWordLabel.frame.size.width, 0, newPassWordView.frame.size.width - (newPassWordLabel.frame.origin.x + newPassWordLabel.frame.size.width) - 10, 44)];
    newPassWordTextField.secureTextEntry = YES;
    
    newPassWordTextField.placeholder = inputNewPassWord;
    newPassWordTextField.font = [UIFont systemFontOfSize:14];
    newPassWordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [newPassWordTextField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [newPassWordView addSubview:newPassWordTextField];
    
    // 确认密码
    confirmPassWordView = [[UIView alloc]initWithFrame:CGRectMake(0, newPassWordView.frame.origin.y + newPassWordView.frame.size.height, SCREENWIDTH, 44)];
    confirmPassWordView.backgroundColor = [UIColor whiteColor];
    [backView addSubview:confirmPassWordView];
    
    UILabel *confirmPassWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 140, 44)];
    confirmPassWordLabel.font = [UIFont systemFontOfSize:14];
    confirmPassWordLabel.text = confirmPassWord;
    [confirmPassWordView addSubview:confirmPassWordLabel];
    
    confirmPassWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(confirmPassWordLabel.frame.origin.x + confirmPassWordLabel.frame.size.width, 0, confirmPassWordView.frame.size.width - (confirmPassWordLabel.frame.origin.x + confirmPassWordLabel.frame.size.width) - 10, 44)];
    confirmPassWordTextField.secureTextEntry = YES;
    confirmPassWordTextField.placeholder = inputConfirmPassWord;
    confirmPassWordTextField.font = [UIFont systemFontOfSize:14];
    confirmPassWordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [confirmPassWordTextField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [confirmPassWordView addSubview:confirmPassWordTextField];
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, newPassWordView.frame.size.height - 0.5, SCREENWIDTH, 0.5)];
    line2.backgroundColor = [UIColor lightGrayColor];
    [newPassWordView addSubview:line2];
  
    NSString *change = NSLocalizedString(@"change", nil);
    // 右按钮
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:change style:UIBarButtonItemStyleDone target:self action:@selector(confirmOldPassWord)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissmissKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    oldPassWordTextField.returnKeyType = UIReturnKeyDone;
    newPassWordTextField.returnKeyType = UIReturnKeyDone;
    confirmPassWordTextField.returnKeyType = UIReturnKeyDone;
    oldPassWordTextField.delegate = self;
    newPassWordTextField.delegate = self;
    confirmPassWordTextField.delegate = self;
    oldPassWordTextField.tag = 3000;
    newPassWordTextField.tag = 3001;
    confirmPassWordTextField.tag = 3002;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField.tag == 3000) {
        [newPassWordTextField becomeFirstResponder];
    }
    if (textField.tag == 3001) {
        [confirmPassWordTextField becomeFirstResponder];
    }
    if (textField.tag == 3002) {
        [self confirmOldPassWord];
        [confirmPassWordTextField resignFirstResponder];
    }
    return YES;
}
#pragma mark - 先检验两次输入的原密码是否一致
- (void)confirmOldPassWord{
    
    NSString *twoTimes = NSLocalizedString(@"twoTimesPassWordIsNotSame", nil);
    NSString *reenter = NSLocalizedString(@"pleaseReenter", nil);
    
    if ([newPassWordTextField.text isEqualToString:confirmPassWordTextField.text]) {
        // 进行网络请求
        [oldPassWordTextField resignFirstResponder];
        [newPassWordTextField resignFirstResponder];
        [confirmPassWordTextField resignFirstResponder];

        [self onTouchModify];
        
    }else{
        [DAAlertView showDAAlertWithPositionType:DAAlertDisplayerPositionTypeCenter animationType:DAAlertDisplayerShowTypeBounce titleText:@"提示" contentText:twoTimes cancelButtonTitle:reenter buttonList:nil handleActionBlock:^(DAAlertView *displayer, NSInteger index) {
            if (index == 999) {
                confirmPassWordTextField.text = nil;
                newPassWordTextField.text = nil;
                [newPassWordTextField becomeFirstResponder];
            }
            
        }];
    }
}
- (void)onTouchModify{
    
    NSString *reLogin = NSLocalizedString(@"modifyTheSuccessRelogin", nil);
    NSString *passWordError = NSLocalizedString(@"oldPassWordError", nil);
    NSString *url = DAChangePassWordURl;
    NSDictionary *dic = @{@"userID": self.userID,
                          @"passWord": oldPassWordTextField.text,
                          @"newPassWord": confirmPassWordTextField.text};
    [DADataHandel postURL:url parame:dic cookie:nil result:^(id result) {
        if ([result[@"result"] isKindOfClass:[NSString class]]) {
            if ([result[@"result"]isEqualToString:@"true"]) {
                NSLog(@"修改成功");
                [SVProgressHUD showSuccessWithStatus:reLogin];
                [self goBackLoginPage];
            }else{
                NSLog(@"修改失败");
                [SVProgressHUD showErrorWithStatus:passWordError];
                oldPassWordTextField.text = nil;
                newPassWordTextField.text = nil;
                confirmPassWordTextField.text = nil;
                [oldPassWordTextField becomeFirstResponder];
            }
        }
    }];
    
}

#pragma mark - 回到登录页面
- (void)goBackLoginPage{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIStoryboard *login = [UIStoryboard storyboardWithName:@"DALogin" bundle:nil];
        self.view.window.rootViewController = login.instantiateInitialViewController;
        [SVProgressHUD dismiss];
    });
    
}



- (void)dissmissKeyboard{
    [oldPassWordTextField resignFirstResponder];
    [newPassWordTextField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
